// Package client
// Created by GoLand
// @User: lenora
// @Date: 2024/8/27
// @Time: 17:14

package client

import (
	intrv1 "camp/microbook/api/proto/gen/interactive"
	"camp/microbook/interactive/domain"
	"camp/microbook/interactive/service"
	"context"
	"google.golang.org/grpc"
)

type InteractiveAdapter struct {
	svc service.InteractiveService
}

func NewInteractiveAdapter(svc service.InteractiveService) intrv1.InteractiveServiceClient {
	return &InteractiveAdapter{svc: svc}
}

func (a *InteractiveAdapter) IncrReadCnt(ctx context.Context, in *intrv1.IncrReadCntReq, opts ...grpc.CallOption) (*intrv1.Response, error) {
	err := a.svc.IncrReadCnt(ctx, in.GetBiz(), in.GetBizId())
	return new(intrv1.Response), err
}

func (a *InteractiveAdapter) IncrLikeCnt(ctx context.Context, in *intrv1.LikeCntReq, opts ...grpc.CallOption) (*intrv1.Response, error) {
	err := a.svc.IncrLikeCnt(ctx, in.GetBiz(), in.GetId(), in.GetUserId())
	return new(intrv1.Response), err
}

func (a *InteractiveAdapter) DecrLikeCnt(ctx context.Context, in *intrv1.LikeCntReq, opts ...grpc.CallOption) (*intrv1.Response, error) {
	err := a.svc.DecrLikeCnt(ctx, in.GetBiz(), in.GetId(), in.GetUserId())
	return new(intrv1.Response), err
}

func (a *InteractiveAdapter) Collect(ctx context.Context, in *intrv1.CollectReq, opts ...grpc.CallOption) (*intrv1.Response, error) {
	err := a.svc.Collect(ctx, in.GetBiz(), in.GetId(), in.GetCid(), in.GetUserId())
	return new(intrv1.Response), err
}

func (a *InteractiveAdapter) Get(ctx context.Context, in *intrv1.GetReq, opts ...grpc.CallOption) (*intrv1.GetResp, error) {
	res, err := a.svc.Get(ctx, in.GetBiz(), in.GetId(), in.GetUserId())
	if err != nil {
		return nil, err
	}
	return &intrv1.GetResp{
		Interactive: a.toDTO(res),
	}, nil
}

func (a *InteractiveAdapter) GetByIds(ctx context.Context, in *intrv1.GetByIdsReq, opts ...grpc.CallOption) (*intrv1.GetByIdsResp, error) {
	res, err := a.svc.GetByIds(ctx, in.GetBiz(), in.GetIds())
	if err != nil {
		return nil, err
	}
	data := make(map[uint64]*intrv1.Interactive, len(res))
	for _, v := range res {
		data[v.BizId] = a.toDTO(v)
	}
	return &intrv1.GetByIdsResp{
		Res: data,
	}, nil
}

func (a *InteractiveAdapter) toDTO(interactive domain.Interactive) *intrv1.Interactive {
	return &intrv1.Interactive{
		BizId:      interactive.BizId,
		ReadCnt:    interactive.ReadCnt,
		LikeCnt:    interactive.LikeCnt,
		Liked:      interactive.Liked,
		CollectCnt: interactive.CollectCnt,
		Collected:  interactive.Collected,
	}
}
